﻿namespace SsisLdapSource.InstantiationTime
{
    using System.Collections.Generic;

    using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

    public class CustomPropertiesInitializer
    {
        private readonly IDTSComponentMetaData100 componentMetaData;
        private readonly IEnumerable<CustomPropertyInfo> customProperties;

        public CustomPropertiesInitializer(IDTSComponentMetaData100 componentMetaData)
        {
            this.componentMetaData = componentMetaData;
            this.customProperties = new CustomProperties().GetCustomProperties;
        }

        public void AddCustomProperties()
        {
            foreach (var customProperty in this.customProperties)
            {
                this.CreateComponentCustomProperty(customProperty);
            }
        }

        private void CreateComponentCustomProperty(CustomPropertyInfo propertyInfo)
        {
            var customProperty = this.componentMetaData.CustomPropertyCollection.New();
            customProperty.Name = propertyInfo.Name;
            customProperty.Description = propertyInfo.Description;
            customProperty.EncryptionRequired = propertyInfo.IsEncryptionRequired;
            customProperty.Value = propertyInfo.DefaultValue;
            customProperty.ExpressionType = DTSCustomPropertyExpressionType.CPET_NOTIFY;
            if (propertyInfo.IsCommaDelimitedToStringArrayTypeConverterRequired)
            {
                customProperty.TypeConverter = typeof(CommaDelimitedToStringArrayTypeConverter).AssemblyQualifiedName;
            }
        }
    }
}
